/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.upao.util;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.upao.ocw.Requerimiento;
import java.util.List;
/**
 *
 * @author JoseHernanQC
 */
public class BDRequerimiento {
    
    public static void saveRequerimiento(Requerimiento requerimiento) {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Transaction txn = datastore.beginTransaction();
        try {
            Entity requer = new Entity("Requerimiento");
            requer.setProperty("nombre", requerimiento.getNombre());
            requer.setProperty("fecha", requerimiento.getFecha());
            requer.setProperty("prioridad", requerimiento.getPrioridad());
            requer.setProperty("descripcion", requerimiento.getDescripcion());
            requer.setProperty("estado", requerimiento.getEstado());
            datastore.put(requer);
            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
    }
    
    public static List<Entity> getAllRequerimientos() {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        Transaction txn = ds.beginTransaction();
        Query q = new Query("Requerimiento");
        PreparedQuery pq;
        try {
            pq = ds.prepare(q);
            txn.commit();
        } finally {
            if (txn.isActive()) {
                txn.rollback();
            }
        }
        return pq.asList(FetchOptions.Builder.withLimit(10));
    }
}
